package com.guodu.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.guodu.dao.UnitsDao;
import com.guodu.dao.ViewMaterialChukuDao;
import com.guodu.dao.ViewMaterialKucunYearDao;
import com.guodu.entity.Units;
import com.guodu.entity.ViewMaterialChuku;
import com.guodu.entity.ViewMaterialKucunYear;
import com.guodu.service.ViewMaterialKucunYearService;
import com.guodu.vo.ViewMaterialChukuVo;
import com.guodu.vo.ViewMaterialKucunYearVo;
import com.guodu.vo.ViewVo;

@Service("viewMaterialKucunYearService")
public class ViewMaterialKucunYearServiceImpl implements
		ViewMaterialKucunYearService {
	
	@Resource
	private ViewMaterialKucunYearDao viewMaterialKucunYearDao;
	
	@Resource
	private UnitsDao unitsDao;
	
	@Override
	public Map<String, ViewMaterialKucunYearVo> findByVo(ViewVo vvo) {
		// TODO Auto-generated method stub
		List<Units> funitses = unitsDao.findAllFunits();
		List<ViewMaterialKucunYear> views = viewMaterialKucunYearDao.findByVo(vvo);
		Map<String, ViewMaterialKucunYearVo> map = new HashMap<String, ViewMaterialKucunYearVo>();
		map.put("all", new ViewMaterialKucunYearVo(new ViewMaterialKucunYear()));
		for(Units units : funitses){
			map.put(units.getUnitsId().toString()+"all", new ViewMaterialKucunYearVo(new ViewMaterialKucunYear()));
		}
		for(ViewMaterialKucunYear view:views){
			this.sum(map.get("all"), view);
			this.sum(map.get(view.getFunitsId().toString()+"all"), view);
			map.put(view.getUnitsId().toString(), new ViewMaterialKucunYearVo(view));
		}
		return map;
	}

	
	private ViewMaterialKucunYearVo sum(ViewMaterialKucunYearVo view1,ViewMaterialKucunYear view2){
		view1.setOneMoney((view1.getOneMoney()!=null?view1.getOneMoney():new BigDecimal(0)).add(view2.getOneMoney()!=null?view2.getOneMoney():new BigDecimal(0)));
		view1.setTwoMoney((view1.getTwoMoney()!=null?view1.getTwoMoney():new BigDecimal(0)).add(view2.getTwoMoney()!=null?view2.getTwoMoney():new BigDecimal(0)));
		view1.setThreeMoney((view1.getThreeMoney()!=null?view1.getThreeMoney():new BigDecimal(0)).add(view2.getThreeMoney()!=null?view2.getThreeMoney():new BigDecimal(0)));
		view1.setFourMoney((view1.getFourMoney()!=null?view1.getFourMoney():new BigDecimal(0)).add(view2.getFourMoney()!=null?view2.getFourMoney():new BigDecimal(0)));
		view1.setFiveMoney((view1.getFiveMoney()!=null?view1.getFiveMoney():new BigDecimal(0)).add(view2.getFiveMoney()!=null?view2.getFiveMoney():new BigDecimal(0)));
		view1.setSixMoney((view1.getSixMoney()!=null?view1.getSixMoney():new BigDecimal(0)).add(view2.getSixMoney()!=null?view2.getSixMoney():new BigDecimal(0)));
		view1.setSevenMoney((view1.getSevenMoney()!=null?view1.getSevenMoney():new BigDecimal(0)).add(view2.getSevenMoney()!=null?view2.getSevenMoney():new BigDecimal(0)));
		view1.setEightMoney((view1.getEightMoney()!=null?view1.getEightMoney():new BigDecimal(0)).add(view2.getEightMoney()!=null?view2.getEightMoney():new BigDecimal(0)));
		view1.setNineMoney((view1.getNineMoney()!=null?view1.getNineMoney():new BigDecimal(0)).add(view2.getNineMoney()!=null?view2.getNineMoney():new BigDecimal(0)));
		view1.setTenMoney((view1.getTenMoney()!=null?view1.getTenMoney():new BigDecimal(0)).add(view2.getTenMoney()!=null?view2.getTenMoney():new BigDecimal(0)));
		view1.setElevenMoney((view1.getElevenMoney()!=null?view1.getEightMoney():new BigDecimal(0)).add(view2.getEightMoney()!=null?view2.getEightMoney():new BigDecimal(0)));
		view1.setTwlevMoney((view1.getTwelveMoney()!=null?view1.getTwelveMoney():new BigDecimal(0)).add(view2.getTwelveMoney()!=null?view2.getTwelveMoney():new BigDecimal(0)));
		return view1;
	}
}
